package service

import (
	"awesomeProject/internal/model"
	"awesomeProject/internal/repository"
	"awesomeProject/pkg/errors"
	"context"
	"strings"
	"time"
)

type CommentService interface {
	CreateComment(ctx context.Context, userID uint, req *CreateCommentRequest) (*model.Comment, error)
	GetComment(ctx context.Context, commentID uint) (*model.Comment, error)
	UpdateComment(ctx context.Context, userID, commentID uint, req *UpdateCommentRequest) error
	DeleteComment(ctx context.Context, userID, commentID uint) error
	GetCommentsByPost(ctx context.Context, postID uint, page, pageSize int) ([]*model.Comment, int64, error)
	GetCommentsByUser(ctx context.Context, userID uint, page, pageSize int) ([]*model.Comment, int64, error)
	GetReplies(ctx context.Context, parentID uint) ([]*model.Comment, error)
}

type commentService struct {
	commentRepo repository.CommentRepository
	postRepo    repository.PostRepository
	userRepo    repository.UserRepository
}

// NewCommentService 创建评论服务实例
func NewCommentService(
	commentRepo repository.CommentRepository,
	postRepo repository.PostRepository,
	userRepo repository.UserRepository,
) CommentService {
	return &commentService{
		commentRepo: commentRepo,
		postRepo:    postRepo,
		userRepo:    userRepo,
	}
}

// 请求结构体定义
type CreateCommentRequest struct {
	PostID   uint   `json:"post_id" binding:"required"`
	ParentID uint   `json:"parent_id" binding:"omitempty"`
	Content  string `json:"content" binding:"required,min=1,max=1000"`
}

type UpdateCommentRequest struct {
	Content string `json:"content" binding:"required,min=1,max=1000"`
}

// CreateComment 创建评论
func (s *commentService) CreateComment(ctx context.Context, userID uint, req *CreateCommentRequest) (*model.Comment, error) {
	// 验证用户是否存在
	_, err := s.userRepo.GetByID(ctx, userID)
	if err != nil {
		return nil, err
	}

	// 验证帖子是否存在
	_, err = s.postRepo.GetByID(ctx, req.PostID)
	if err != nil {
		return nil, err
	}

	// 如果是回复评论，验证父评论是否存在
	if req.ParentID != 0 {
		_, err = s.commentRepo.GetByID(ctx, req.ParentID)
		if err != nil {
			return nil, err
		}
	}

	comment := &model.Comment{
		PostID:    req.PostID,
		UserID:    userID,
		ParentID:  &req.ParentID,
		Content:   strings.TrimSpace(req.Content),
		Status:    1, // 默认发布状态
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}

	if err := s.commentRepo.Create(ctx, comment); err != nil {
		return nil, err
	}

	return comment, nil
}

// GetComment 获取评论详情
func (s *commentService) GetComment(ctx context.Context, commentID uint) (*model.Comment, error) {
	return s.commentRepo.GetByID(ctx, commentID)
}

// UpdateComment 更新评论
func (s *commentService) UpdateComment(ctx context.Context, userID, commentID uint, req *UpdateCommentRequest) error {
	comment, err := s.commentRepo.GetByID(ctx, commentID)
	if err != nil {
		return err
	}

	// 验证权限：只有作者可以修改
	if comment.UserID != userID {
		return errors.ErrCommentNotOwner
	}

	comment.Content = strings.TrimSpace(req.Content)
	comment.UpdatedAt = time.Now()

	return s.commentRepo.Update(ctx, comment)
}

// DeleteComment 删除评论
func (s *commentService) DeleteComment(ctx context.Context, userID, commentID uint) error {
	comment, err := s.commentRepo.GetByID(ctx, commentID)
	if err != nil {
		return err
	}

	// 验证权限：只有作者可以删除
	if comment.UserID != userID {
		return errors.ErrCommentNotOwner
	}

	return s.commentRepo.Delete(ctx, commentID)
}

// GetCommentsByPost 获取帖子的评论列表
func (s *commentService) GetCommentsByPost(ctx context.Context, postID uint, page, pageSize int) ([]*model.Comment, int64, error) {
	// 验证帖子是否存在
	_, err := s.postRepo.GetByID(ctx, postID)
	if err != nil {
		return nil, 0, err
	}

	// 参数验证
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 || pageSize > 100 {
		pageSize = 20
	}

	return s.commentRepo.ListByPost(ctx, postID, page, pageSize)
}

// GetCommentsByUser 获取用户的评论列表
func (s *commentService) GetCommentsByUser(ctx context.Context, userID uint, page, pageSize int) ([]*model.Comment, int64, error) {
	// 验证用户是否存在
	_, err := s.userRepo.GetByID(ctx, userID)
	if err != nil {
		return nil, 0, err
	}

	// 参数验证
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 || pageSize > 100 {
		pageSize = 20
	}

	return s.commentRepo.ListByUser(ctx, userID, page, pageSize)
}

// GetReplies 获取评论的回复列表
func (s *commentService) GetReplies(ctx context.Context, parentID uint) ([]*model.Comment, error) {
	// 验证父评论是否存在
	_, err := s.commentRepo.GetByID(ctx, parentID)
	if err != nil {
		return nil, err
	}

	return s.commentRepo.GetReplies(ctx, parentID)
}
